package com.traffic.api.controller;

import com.traffic.api.entity.AlipayUser;
import com.traffic.api.service.AlipayUserService;
import com.traffic.api.util.BaseResponse;
import com.traffic.api.util.ResultCodeAndMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by churcy on 2017/3/17.
 */
@Controller
@RequestMapping(value = "/local/user")
public class UserController {

    @Autowired
    private AlipayUserService alipayUserService;

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Object get(HttpServletRequest request) {
        Map<String, Object> obj = new HashMap<>();
        String userId = request.getParameter("userId");
        try {
            do {
                if (StringUtils.isAnyBlank(userId)) {
                    obj = BaseResponse.setResponse(null, ResultCodeAndMessage.PARAM_ERROR);
                    break;
                }
                Map<String, Object> userConditions = new HashMap<>();
                userConditions.put("userId", userId);
                List<AlipayUser> alipayUsers = alipayUserService.findByConditions(userConditions);
                if (alipayUsers == null || alipayUsers.size() == 0) {
                    break;
                }

                Map<String, Object> data = new HashMap<>();
                data.put("user", alipayUsers.get(0));
                obj = BaseResponse.setResponse(data, ResultCodeAndMessage.SUCCESS);
            } while (false);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj;
    }

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public Object update(HttpServletRequest request) {
        Map<String, Object> obj = new HashMap<>();
        String userId = request.getParameter("userId");
        String source = request.getParameter("source");//来源
        String userName = request.getParameter("userName");
        try {
            do {
                if (StringUtils.isAnyBlank(userId)) {
                    obj = BaseResponse.setResponse(null, ResultCodeAndMessage.PARAM_ERROR);
                    break;
                }
                Map<String, Object> userConditions = new HashMap<>();
                userConditions.put("userId", userId);
                List<AlipayUser> alipayUsers = alipayUserService.findByConditions(userConditions);
                AlipayUser alipayUser;
                if (alipayUsers == null || alipayUsers.size() == 0) {
                    alipayUser = new AlipayUser();
                    alipayUser.setUserId(userId);
                    alipayUser.setUserName(userName);
                } else {
                    alipayUser = alipayUsers.get(0);
                }
                alipayUser.setIsServer("T");
                alipayUser.setServerTime(new Date());
                alipayUserService.update(alipayUser);
                Map<String, Object> data = new HashMap<>();
                data.put("userId", userId);
                data.put("userName", userName);
                obj = BaseResponse.setResponse(data, ResultCodeAndMessage.SUCCESS);
            } while (false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

}
